package com.iad.service;

import java.util.List;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.servlet.ServletContext;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.iad.util.Mail;
import com.iad.entity.*;
import com.iad.persistence.SiteDAO;
import com.iad.core.Core;

public class SignupService {
  private ServletContext context;
  private WebApplicationContext wac;
  private SiteDAO siteDAO;
  
	private static Log theLog = LogFactory.getLog(SiteService.class);
  
  /**
   * Default Constructor
   */
  public SignupService(){
      context = flex.messaging.FlexContext.getServletContext();
      wac = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
      siteDAO = (SiteDAO) wac.getBean("siteDAO");
  }
  
  public Long sendContactMessage(ContactMessage contactMessage) throws Exception {
    String email = contactMessage.getAuthor();
    Long id = siteDAO.newContactMessage(contactMessage);
    Mail.mailContactMessage(
        "\"IAD Support\" <support@independentartistsdirect.com>",
        "\"IAD Contact Form\" <support@independentartistsdirect.com>",
        "Message from " + email + ": " + contactMessage.getSubject(),
        "text/html", contactMessage);
    return id;
  }
  
  public void sendWelcomeEmail(WelcomeEmail email) throws Exception {
    Mail.mailWelcomeEmail("text/html", email);
  }

}
